Skip to content

Conversation

@rixafy
Copy link
Contributor

@rixafy rixafy commented May 25, 2025

  • new feature
  • BC break? no (InjectExtension::getInjectProperties changed - but it was marked as internal)

I also added new method getByTypeAndTag (container + autowiring) so $throw can be always as last parameter, otherwise it would be getByType($type, $throw, $tag).

Now it's possible to have multiple services of the same type and inject them by tag with #[Inject(tag: <tag>), it's also possible to use in constructors.

Only rule is that one of those multiple services needs to have tag default, and that service will be injected if no tag is specified (or if you specify default). I have considered also to inject service with least amount of tags or with no tags, but this can lead to unpredictable behavior when more tags are added in the future.

This allows to work with interfaces and requiring specific services, some examples:

  • cache implementations
  • doctrine connections
  • flysystem implementations
  • monolog channels
final class MyService
{
	#[Inject('slack')] 
	public LoggerInterface $slackLogger;

	public function __construct(
		private Connection $connection,

		#[Inject('secondary')] 
		private Connection $secondaryConnection,

		private Cache $cache,

		#[Inject('asset')] 
		private Cache $assetCache,
	) {}
}

@rixafy rixafy force-pushed the inject-with-tags branch from 2b76506 to a79adc1 Compare May 25, 2025 18:09
@dg dg force-pushed the v3.2 branch 2 times, most recently from 5e952c3 to b83c80a Compare June 5, 2025 23:27
@rixafy rixafy force-pushed the inject-with-tags branch from a79adc1 to 6c73aa4 Compare June 13, 2025 17:18
@rixafy rixafy changed the base branch from v3.2 to v3.3 June 18, 2025 18:38
@rixafy rixafy force-pushed the inject-with-tags branch from 6c73aa4 to b97b8fe Compare June 18, 2025 18:40
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants